const db = wx.cloud.database();
const app = getApp();
Page({
  data: {},
  onShow(){
    db.collection('userInfo').get({
      success: res => {
        // console.log( {
        //   name: userValue.userName,
        //   _class: userValue.userClass,
        //   number: userValue.userNumber,
        //   phone: userValue.userPhone,
        //   score:0,
        //   order:{
        //     tea:'',
        //     place:'',
        //     time:'',
        //   }
        // },res.data[0]._id)
        this.setData({
          name: res.data[0].name,
          _class: res.data[0]._class,
          number: res.data[0].number,
          phone: res.data[0].phone,
        })

      }
    })

   
  },
  formSubmit: function(res) {
    console.log(res);
    const userValue = res.detail.value;
    if ((userValue.userClass && userValue.userName && userValue.userPhone && userValue.userNumber) == '') {
      wx.showModal({
        title: '错误',
        content: '请将所有信息填写完整！',
      })
    } else {
      wx.showModal({
        title: '确认信息',
        content: '请确认所有信息是否正确',
        success: res => {
          if (res.confirm) {
            db.collection('userInfo').get({
              success: res => {
                wx.showLoading({
                  title: '提交中',
                  mask: true,
                })
                console.log(res.data)
                db.collection('userInfo').doc(res.data[0]._id).update({
                  data: {
                    name: userValue.userName,
                    _class: userValue.userClass,
                    number: userValue.userNumber,
                    phone: userValue.userPhone
                  },fail:res=>{
                    console.log(res)
                  },success:res=>{
                    console.log(res)
                  }
                })
                setTimeout(function() {
                  wx.hideLoading()
                }, 1000)
                wx.reLaunch({
                  url: '../index/index',
                })
              }
            })
          } else if (res.cancel) {
            return;
          }
        }
      })
    }
  }
})